package pers.agony.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import pers.agony.constant.MessageConstant;
import pers.agony.entity.Result;
import pers.agony.exception.MyException;
import pers.agony.pojo.OrderSetting;
import pers.agony.service.OrderSettingService;
import pers.agony.utils.POIUtils;

import java.io.IOException;
import java.util.*;

/**
 * @author : Agony
 * @date : Created in 11:44 上午 2021/11/4
 * @Description:
 * @Modified By:
 * @version: : 1.0
 */
@RestController
public class OrderSettingController {

    @Autowired
    private OrderSettingService orderSettingService;

    @PostMapping("/ordersetting/upload.do")
    public Result upload(MultipartFile excelFile) {
        //调用POIUtils读取excel内容
        try {
            List<String[]> list = POIUtils.readExcel(excelFile);
            ArrayList<OrderSetting> orderList = new ArrayList<OrderSetting>(list.size());
            //转成List<Ordersetting>实体类
            for (String[] strings : list) {
                String orderDate1 = strings[0];
                String number1 = strings[1];
                OrderSetting os = new OrderSetting(new Date(orderDate1), Integer.valueOf(number1));
                orderList.add(os);
            }
            //调用业务导入
            orderSettingService.doImport(orderList);
            //返回结果给页面
            return new Result(true, MessageConstant.IMPORT_ORDERSETTING_SUCCESS);
        } catch (MyException e) {
            e.printStackTrace();
            return new Result(false, e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.IMPORT_ORDERSETTING_FAIL);
        }
    }

    /**
     * 根据日期查询预约设置数据(获取指定日期所在月份的预约设置数据)
     *
     * @param date
     * @return
     */
    @PostMapping("/ordersetting/getOrderSettingByMonth.do")
    public Result getOrderSettingByMonth(String date) {//参数格式为：2019-03
        try {
            List<Map> list = orderSettingService.getOrderSettingByMonth(date);
            //获取预约设置数据成功
            return new Result(true, MessageConstant.GET_ORDERSETTING_SUCCESS, list);
        } catch (Exception e) {
            e.printStackTrace();
            //获取预约设置数据失败
            return new Result(false, MessageConstant.GET_ORDERSETTING_FAIL);
        }
    }

        /**
         * 根据指定日期修改可预约人数
         * @param orderSetting
         * @return
         */
        @PostMapping("/ordersetting/editNumberByDate.do")
        public Result editNumberByDate(@RequestBody OrderSetting orderSetting){
            try{
                orderSettingService.editNumberByDate(orderSetting);
                //预约设置成功
                return new Result(true,MessageConstant.ORDERSETTING_SUCCESS);
            }catch (Exception e){
                e.printStackTrace();
                //预约设置失败
                return new Result(false,MessageConstant.ORDERSETTING_FAIL);
            }
        }
}
